package com.atguigu.guli.service.edu.service.impl;

import com.atguigu.guli.service.base.dto.CourseDto;
import com.atguigu.guli.service.edu.entity.Course;
import com.atguigu.guli.service.edu.entity.CourseDescription;
import com.atguigu.guli.service.edu.entity.CourseInfoForm;
import com.atguigu.guli.service.edu.entity.query.ApiCourseQuery;
import com.atguigu.guli.service.edu.entity.search.CourseInfoSearch;
import com.atguigu.guli.service.edu.entity.vo.ApiCourseDetailVo;
import com.atguigu.guli.service.edu.entity.vo.CourseDetailVo;
import com.atguigu.guli.service.edu.mapper.CourseDescriptionMapper;
import com.atguigu.guli.service.edu.mapper.CourseMapper;
import com.atguigu.guli.service.edu.service.CourseService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.util.List;

/**
 * <p>
 * 课程 服务实现类
 * </p>
 *
 * @author leo
 * @since 2021-11-24
 */
@Service
public class CourseServiceImpl extends ServiceImpl<CourseMapper, Course> implements CourseService {
    @Autowired
    private CourseDescriptionMapper courseDescriptionMapper;
    @Transactional(rollbackFor = Exception.class)
    @Override
    public String saveCourseInfo(CourseInfoForm courseInfoForm) {
       //1.保存课程基本信息
        Course course = new Course() ;
        course.setStatus(Course.COURSE_DRAFT) ;
        BeanUtils.copyProperties(courseInfoForm,course);
        baseMapper.insert(course) ;
        // 2.保存课程详细信息
        CourseDescription courseDescription = new CourseDescription() ;
        courseDescription.setDescription(courseInfoForm.getDescription()) ;
        courseDescription.setId(course.getId()) ;
        courseDescriptionMapper.insert(courseDescription) ;
        //3.返回课程id，方便后续页面调用
        return course.getId() ;
    }


    //更新课程
    @Override
    public void updateCourseInfo(CourseInfoForm courseInfoForm, String courseId) {
        Course course = new Course() ;
        BeanUtils.copyProperties(courseInfoForm,course);
        course.setId(courseId) ;
        baseMapper.updateById(course) ;
        CourseDescription courseDescription = new CourseDescription() ;
        courseDescription.setId(courseId) ;
        courseDescription.setDescription(courseInfoForm.getDescription()) ;
        courseDescriptionMapper.updateById(courseDescription) ;
    }

    //带条件分页查询
    @Override
    public void getInfoSearch(Page<CourseDetailVo> page, CourseInfoSearch courseInfoSearch) {
        QueryWrapper<CourseDetailVo> queryWrapper = new QueryWrapper<>() ;
        List<CourseDetailVo> courseDetailVos = null ;

        String title = courseInfoSearch.getTitle();
        String subjectId = courseInfoSearch.getSubjectId();
        String subjectParentId = courseInfoSearch.getSubjectParentId();
        String teacherId = courseInfoSearch.getTeacherId();

        //没有条件，全部查询
        if (StringUtils.isEmpty(title)
                && StringUtils.isEmpty(subjectId)
                && StringUtils.isEmpty(subjectParentId)
                && StringUtils.isEmpty(teacherId)) {
            queryWrapper.last("where 1=1") ;
            courseDetailVos = baseMapper.selectCourseDetailVoPage(page,queryWrapper) ;
            page.setRecords(courseDetailVos) ;
            return;
        }
        //条件查询
        if (!StringUtils.isEmpty(title)) {
            //由于多表联查，多张表都有title字段，如果直接使用title作为列名，执行sql时无法确认使用哪张表的title列
            queryWrapper.like("t1.title",title) ;
        }
        if (!StringUtils.isEmpty(subjectId)) {
            //由于多表联查，多张表都有title字段，如果直接使用title作为列名，执行sql时无法确认使用哪张表的title列
            queryWrapper.eq("subject_id",subjectId) ;
        }
        if (!StringUtils.isEmpty(subjectParentId)) {
            //由于多表联查，多张表都有title字段，如果直接使用title作为列名，执行sql时无法确认使用哪张表的title列
            queryWrapper.eq("subject_parent_id",subjectParentId) ;
        }
        if (!StringUtils.isEmpty(teacherId)) {
            //由于多表联查，多张表都有title字段，如果直接使用title作为列名，执行sql时无法确认使用哪张表的title列
            queryWrapper.eq("teacher_id",teacherId) ;
        }
        courseDetailVos =  baseMapper.selectCourseDetailVoPage(page,queryWrapper) ;
        page.setRecords(courseDetailVos) ;

    }

    @Override
    public List<Course> getCoursesByTeacherId(String teacherId) {
        LambdaQueryWrapper<Course> queryWrapper = new LambdaQueryWrapper<>() ;
        queryWrapper.select(Course::getId,Course::getCover,Course::getTitle) ;
        queryWrapper.eq(Course::getTeacherId,teacherId) ;
        return this.list(queryWrapper);
    }

    @Override
    public List<Course> getCoursesByCondition(ApiCourseQuery apiCourseQuery) {
        String subjectParentId = apiCourseQuery.getSubjectParentId();
        String subjectId = apiCourseQuery.getSubjectId();
        String orderByColumn = apiCourseQuery.getOrderByColumn();
        Integer type = apiCourseQuery.getType();
        QueryWrapper<Course> queryWrapper = new QueryWrapper<>() ;
        if (!StringUtils.isEmpty(subjectParentId)) {
            queryWrapper.eq("subject_parent_id",subjectParentId) ;
        }
        if (!StringUtils.isEmpty(subjectId)) {
            queryWrapper.eq("subject_id",subjectId) ;
        }
        if (type == 0) {
            //降序
            queryWrapper.orderByDesc(orderByColumn) ;
        } else {
            //升序
            queryWrapper.orderByAsc(orderByColumn) ;
        }
        queryWrapper.eq("status","Normal") ;
        return baseMapper.selectList(queryWrapper);
    }

    @Override
    public ApiCourseDetailVo getCourseDetailById(String courseId) {
        //修改课程浏览数量
        ApiCourseDetailVo courseDetailVo = baseMapper.selectCourseDetailById(courseId) ;
        courseDetailVo.setViewCount(courseDetailVo.getViewCount()+1);
        LambdaUpdateWrapper<Course> updateWrapper = new LambdaUpdateWrapper<>() ;
        updateWrapper.set(Course::getViewCount,courseDetailVo.getViewCount()) ;
        updateWrapper.eq(Course::getId,courseId) ;
        this.update(updateWrapper);
        return courseDetailVo ;
    }

    //首页查询热门课程数量（浏览量前8）
    @Override
    @Cacheable(value = "course",key = "'hotCourses'")
    public List<Course> getHotCourses() {
        LambdaQueryWrapper<Course> queryWrapper = new LambdaQueryWrapper<>() ;
        queryWrapper.orderByDesc(Course::getViewCount) ;
        queryWrapper.last("limit 8") ;
        return this.list(queryWrapper);
    }

    @Override
    public CourseDto getCourseDto(String courseId) {
        LambdaQueryWrapper queryWrapper = new LambdaQueryWrapper() ;


        return null;
    }

    /*@Override
    public Page<CourseDetailVo> listPage(Integer pageNum, Integer pageSize) {
        Page<CourseDetailVo> page = new Page<>(pageNum,pageSize) ;
        List<CourseDetailVo> courseDetailVoList = baseMapper.selectCourseDetailVo(page) ;
        page.setRecords(courseDetailVoList) ; //将查询到的分页数据集合设置给分页对象
        return page;
    }*/
}
